package cn.gudian.taglib.cms.customer

import cn.gudian.cms.News.Article
import cn.gudian.cms.News.ArcType

class CusArticleTagLib {
  static namespace = "gd"    //定义该系统的命名空间
  /**
   * 显示某个栏目的内容列表标签
   * 修改默认样式可以修改/tagTemplate/articleList
   */
  def articleList = {attrs, body ->
    def arcTypeID = attrs?.id
    def offset = params.offset?.toInteger()
    def max = params.max?.toInteger()
    if (!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0)
    if (!max) max = (attrs.max ? attrs.max.toInteger() :5)
    def articleInstanceList = null;
    def articleInstanceTotal = null;
    if (arcTypeID == null || arcTypeID == "") {
      articleInstanceList = Article.findAll("from Article where arc_type_id in(select id from ArcType where mode=?) order by id desc", ["Article"], [max: max, offset: offset]);
      articleInstanceTotal = Article.executeQuery("select count(*) from Article where arc_type_id in(select id from ArcType where mode=?)", ["Article"]);
    } else {
      articleInstanceList = Article.findAll("from Article where arc_type_id=? order by id desc", [arcTypeID], [max: max, offset: offset]);
      articleInstanceTotal = Article.executeQuery("select count(*) from Article where arc_type_id=?", [arcTypeID]);
    }
    if (articleInstanceList.size() == 0) {
      out << "暂无内容"
    }
    else {
      out << render(template: '/tagTemplate/articleList', model: [articleInstanceList: articleInstanceList, articleInstanceTotal: articleInstanceTotal[0]])
    }
  }

  /**
   * 显示内容详细信息标签
   * 修改默认样式可以修改/tagTemplate/articleShow
   */
  def articleShow = {attrs, body ->
    def articleID = attrs?.id
    def articleInstance = null
    if (articleID)
      articleInstance = Article.find("from Article where id=? and ismake=true", [Long.parseLong(articleID)]);

    if (articleInstance)
      out << render(template: '/tagTemplate/articleShow', model: [articleInstance: articleInstance])
    else
      out << "内容不存在或者没有通过审核"
  }

}
